{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "12cd8457",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e7c74f1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(precision=6, suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7d509690",
   "metadata": {},
   "outputs": [],
   "source": [
    "# -----------------------------\n",
    "# 1. 准备两个一维样本（标量协方差演示）\n",
    "# -----------------------------\n",
    "x = np.array([2.0, 4.0, 6.0, 8.0])\n",
    "y = np.array([1.0, 3.0, 2.0, 5.0])\n",
    "n = x.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b93cca21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 4., 6., 8.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4d6418c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 3., 2., 5.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "30c852b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4c29b601",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'float' and 'builtin_function_or_method'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m cov_manual_loop \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0.0\u001b[39m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(n):\n\u001b[1;32m----> 7\u001b[0m     cov_manual_loop \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m (x[i] \u001b[38;5;241m-\u001b[39m mx) \u001b[38;5;241m*\u001b[39m (\u001b[43my\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mmy\u001b[49m)\n\u001b[0;32m      8\u001b[0m cov_manual_loop \u001b[38;5;241m/\u001b[39m\u001b[38;5;241m=\u001b[39m (n \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m手工循环计算的样本协方差 cov(x,y) - \u001b[39m\u001b[38;5;124m\"\u001b[39m, cov_manual_loop)\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'float' and 'builtin_function_or_method'"
     ]
    }
   ],
   "source": [
    "# 均值（样本均值）\n",
    "mx = x.mean()\n",
    "my = y.mean\n",
    "# 标量：逐项循环计算 -- 样本方差（除以n-1）\n",
    "cov_manual_loop = 0.0\n",
    "for i in range(n):\n",
    "    cov_manual_loop += (x[i] - mx) * (y[i] - my)\n",
    "cov_manual_loop /= (n - 1)\n",
    "print(\"\\n手工循环计算的样本协方差 cov(x,y) - \", cov_manual_loop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9caa4c06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "手工循环计算的样本协方差 cov(x,y) -  3.6666666666666665\n"
     ]
    }
   ],
   "source": [
    "# 均值（样本均值）\n",
    "mx = x.mean()\n",
    "my = y.mean()\n",
    "\n",
    "# 标量：逐项循环计算 -- 样本方差（除以n-1）\n",
    "cov_manual_loop = 0.0\n",
    "for i in range(n):\n",
    "    cov_manual_loop += (x[i] - mx) * (y[i] - my)\n",
    "cov_manual_loop /= (n - 1)\n",
    "print(\"\\n手工循环计算的样本协方差 cov(x,y) - \", cov_manual_loop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bfb68891",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向量化（点积）计算 cov(x,y) = 3.6666666666666665\n"
     ]
    }
   ],
   "source": [
    "# 标量：向量化（点积）计算（等价）\n",
    "# 两个向量的dot product\n",
    "cov_dot = ((x - mx) @ (y - my)) / (n - 1)\n",
    "print(\"向量化（点积）计算 cov(x,y) =\", cov_dot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ae835ad0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "np.cov(x,y)[0,1] = 3.6666666666666665\n"
     ]
    }
   ],
   "source": [
    "# numpy 内置函数 np.cov(默认是样本方差 归一化为n-1)\n",
    "cov_np = np.cov(x,y,bias=False)[0,1] # [0,1] 是x与y的协方差\n",
    "print(\"np.cov(x,y)[0,1] =\", cov_np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bbdbc4a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "总体（population）协方差（除以 n） = 2.75\n"
     ]
    }
   ],
   "source": [
    "# 同时展示“总体”协方差\n",
    "cov_population = ((x - mx) @ (y - my)) / n\n",
    "print(\"总体（population）协方差（除以 n） =\", cov_population)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e49a9e2a",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'function' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 6\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# -----------------------------\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# 2. 向量化协方差矩阵（多个变量）\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# -----------------------------\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;66;03m# 增加第三个变量 z（可任意），构成数据矩阵 X (n x p)\u001b[39;00m\n\u001b[0;32m      5\u001b[0m z \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m5.0\u001b[39m, \u001b[38;5;241m7.0\u001b[39m, \u001b[38;5;241m9.0\u001b[39m, \u001b[38;5;241m11.0\u001b[39m])\n\u001b[1;32m----> 6\u001b[0m X \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcolumn_stack\u001b[49m\u001b[43m[\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43mz\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m=== 矩阵化示例 ===\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m设计矩阵 X (n x p) =\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, X)\n",
      "\u001b[1;31mTypeError\u001b[0m: 'function' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "# -----------------------------\n",
    "# 2. 向量化协方差矩阵（多个变量）\n",
    "# -----------------------------\n",
    "# 增加第三个变量 z（可任意），构成数据矩阵 X (n x p)\n",
    "z = np.array([5.0, 7.0, 9.0, 11.0])\n",
    "X = np.column_stack[x,y,z]\n",
    "print(\"\\n=== 矩阵化示例 ===\")\n",
    "print(\"设计矩阵 X (n x p) =\\n\", X)\n",
    "print(\"X.shape =\", X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c07f1fb1",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'function' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 6\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# -----------------------------\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# 2. 向量化协方差矩阵（多个变量）\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# -----------------------------\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;66;03m# 增加第三个变量 z（可任意），构成数据矩阵 X (n x p)\u001b[39;00m\n\u001b[0;32m      5\u001b[0m z \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m5.0\u001b[39m, \u001b[38;5;241m7.0\u001b[39m, \u001b[38;5;241m9.0\u001b[39m, \u001b[38;5;241m11.0\u001b[39m])\n\u001b[1;32m----> 6\u001b[0m X \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcolumn_stack\u001b[49m\u001b[43m[\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43mz\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m=== 矩阵化示例 ===\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m设计矩阵 X (n x p) =\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, X)\n",
      "\u001b[1;31mTypeError\u001b[0m: 'function' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "# -----------------------------\n",
    "# 2. 向量化协方差矩阵（多个变量）\n",
    "# -----------------------------\n",
    "# 增加第三个变量 z（可任意），构成数据矩阵 X (n x p)\n",
    "z = np.array([5.0, 7.0, 9.0, 11.0])\n",
    "X = np.column_stack[x,y,z]\n",
    "print(\"\\n=== 矩阵化示例 ===\")\n",
    "print(\"设计矩阵 X (n x p) =\\n\", X)\n",
    "print(\"X.shape =\", X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1c6ea3de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 矩阵化示例 ===\n",
      "设计矩阵 X (n x p) =\n",
      " [[ 2.  1.  5.]\n",
      " [ 4.  3.  7.]\n",
      " [ 6.  2.  9.]\n",
      " [ 8.  5. 11.]]\n",
      "X.shape = (4, 3)\n"
     ]
    }
   ],
   "source": [
    "z = np.array([5.0, 7.0, 9.0, 11.0])\n",
    "X = np.column_stack([x, y, z])   # 每列是一个变量，形状 (n, p)\n",
    "print(\"\\n=== 矩阵化示例 ===\")\n",
    "print(\"设计矩阵 X (n x p) =\\n\", X)\n",
    "print(\"X.shape =\", X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aac19a45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "每列均值 X_mean = [5.   2.75 8.  ]\n",
      "去中心后的 Xc =\n",
      " [[-3.   -1.75 -3.  ]\n",
      " [-1.    0.25 -1.  ]\n",
      " [ 1.   -0.75  1.  ]\n",
      " [ 3.    2.25  3.  ]]\n"
     ]
    }
   ],
   "source": [
    "# 去中心化（每列减去列均值）\n",
    "X_mean = X.mean(axis=0)\n",
    "Xc = X -X_mean\n",
    "print(\"\\n每列均值 X_mean =\", X_mean)\n",
    "print(\"去中心后的 Xc =\\n\", Xc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "88b46d8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "向量化计算的协方差矩阵 S = Xc^T @ Xc / (n-1) =\n",
      " [[6.666667 3.666667 6.666667]\n",
      " [3.666667 2.916667 3.666667]\n",
      " [6.666667 3.666667 6.666667]]\n"
     ]
    }
   ],
   "source": [
    "# 协方差矩阵（样本协方差，除以n-1） - 向量和公式： S = Xc^T Xc / (n - 1)\n",
    "S = Xc.T @ Xc / (n - 1)\n",
    "print(\"\\n向量化计算的协方差矩阵 S = Xc^T @ Xc / (n-1) =\\n\", S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e4c3f4bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.6666666666666665"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9416608e",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2629989112.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[18], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m    XX XY XZ\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "[\n",
    "    XX XY XZ\n",
    "    YX YY YZ\n",
    "    ZX ZY ZZ\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f5bfc503",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构造数据集 (行=样本，列=特征)\n",
    "# 特征： age, income, exercise_hours\n",
    "# 目标： has_disease\n",
    "X = np.array([\n",
    "    [18, 2000, 10, 0],\n",
    "    [22, 2500,  8, 0],\n",
    "    [25, 3000,  7, 0],\n",
    "    [30, 4000,  6, 0],\n",
    "    [40, 6000,  4, 1],\n",
    "    [50, 8000,  3, 1],\n",
    "    [60,10000,  2, 1],\n",
    "    [70,12000,  1, 1],\n",
    "])\n",
    "\n",
    "# 拆分为特征矩阵和目标向量\n",
    "features = X[:, :-1]   # age, income, exercise_hours\n",
    "target   = X[:, -1]    # has_disease"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "aeab46a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   18,  2000,    10],\n",
       "       [   22,  2500,     8],\n",
       "       [   25,  3000,     7],\n",
       "       [   30,  4000,     6],\n",
       "       [   40,  6000,     4],\n",
       "       [   50,  8000,     3],\n",
       "       [   60, 10000,     2],\n",
       "       [   70, 12000,     1]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "91d817ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0bf34d0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算每个特征与目标的相关系数\n",
    "\n",
    "# 中心化\n",
    "features_centered = features - features.mean(axis=0)\n",
    "target_centered = target - target.mean()\n",
    "\n",
    "# 分子 每个特征与目标向量的协方差\n",
    "numerator =  features_centered.T @ target_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a694aab1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   62.5, 12250. ,   -10.5])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e3b326c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12250.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerator[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "885e52e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  -21.375, -3937.5  ,     4.875],\n",
       "       [  -17.375, -3437.5  ,     2.875],\n",
       "       [  -14.375, -2937.5  ,     1.875],\n",
       "       [   -9.375, -1937.5  ,     0.875],\n",
       "       [    0.625,    62.5  ,    -1.125],\n",
       "       [   10.625,  2062.5  ,    -2.125],\n",
       "       [   20.625,  4062.5  ,    -3.125],\n",
       "       [   30.625,  6062.5  ,    -4.125]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "32f06de3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[     456.890625, 15503906.25    ,       23.765625],\n",
       "       [     301.890625, 11816406.25    ,        8.265625],\n",
       "       [     206.640625,  8628906.25    ,        3.515625],\n",
       "       [      87.890625,  3753906.25    ,        0.765625],\n",
       "       [       0.390625,     3906.25    ,        1.265625],\n",
       "       [     112.890625,  4253906.25    ,        4.515625],\n",
       "       [     425.390625, 16503906.25    ,        9.765625],\n",
       "       [     937.890625, 36753906.25    ,       17.015625]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_centered ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8b1320ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[    2529.875,   495687.5  ,     -403.375],\n",
       "       [  495687.5  , 97218750.   ,   -78437.5  ],\n",
       "       [    -403.375,   -78437.5  ,       68.875]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features_centered.T @ features_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8b36fc6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "97221348.75"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(features_centered ** 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "24ab39bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9860.088678607308"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.sum(features_centered ** 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8b34a766",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4142135623730951"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.sum(target_centered ** 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "63d7dc93",
   "metadata": {},
   "outputs": [],
   "source": [
    "denominator = np.sqrt(np.sum(features_centered ** 2)) * np.sqrt(np.sum(target_centered ** 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "17730925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.004482,  0.878497, -0.000753])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerator / denominator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0ef21de8",
   "metadata": {},
   "outputs": [],
   "source": [
    "points = np.arange(-5, 5, 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c51b4462",
   "metadata": {},
   "outputs": [],
   "source": [
    "xs, ys = np.meshgrid(points, points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f1fec3a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],\n",
       "        [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],\n",
       "        [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],\n",
       "        ...,\n",
       "        [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],\n",
       "        [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99],\n",
       "        [-5.  , -4.99, -4.98, ...,  4.97,  4.98,  4.99]]),\n",
       " array([[-5.  , -5.  , -5.  , ..., -5.  , -5.  , -5.  ],\n",
       "        [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99],\n",
       "        [-4.98, -4.98, -4.98, ..., -4.98, -4.98, -4.98],\n",
       "        ...,\n",
       "        [ 4.97,  4.97,  4.97, ...,  4.97,  4.97,  4.97],\n",
       "        [ 4.98,  4.98,  4.98, ...,  4.98,  4.98,  4.98],\n",
       "        [ 4.99,  4.99,  4.99, ...,  4.99,  4.99,  4.99]]))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs,ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "133e2e16",
   "metadata": {},
   "outputs": [],
   "source": [
    "z = np.sqrt(xs ** 2 + ys ** 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f0bd4ffe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7.071068, 7.064   , 7.05694 , ..., 7.049887, 7.05694 , 7.064   ],\n",
       "       [7.064   , 7.056926, 7.049858, ..., 7.042798, 7.049858, 7.056926],\n",
       "       [7.05694 , 7.049858, 7.042784, ..., 7.035716, 7.042784, 7.049858],\n",
       "       ...,\n",
       "       [7.049887, 7.042798, 7.035716, ..., 7.028641, 7.035716, 7.042798],\n",
       "       [7.05694 , 7.049858, 7.042784, ..., 7.035716, 7.042784, 7.049858],\n",
       "       [7.064   , 7.056926, 7.049858, ..., 7.042798, 7.049858, 7.056926]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e2f19253",
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy.linalg import inv, qr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8dea446b",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.random.randn(5,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "f119ddb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "mat = X.T.dot(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "bbea95a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.046068, -0.460404, -1.453244, -1.730284, -1.621981],\n",
       "       [-0.460404, 10.404395,  2.357198, -1.465957, -0.217077],\n",
       "       [-1.453244,  2.357198,  2.565374,  1.607247, -0.17368 ],\n",
       "       [-1.730284, -1.465957,  1.607247,  5.859279, -0.319205],\n",
       "       [-1.621981, -0.217077, -0.17368 , -0.319205,  3.003756]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "7207612c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.511263,  1.856601,  1.209131, -0.452016,  0.140602],\n",
       "       [ 0.435096, -1.118819, -0.497072, -0.002354, -0.0527  ],\n",
       "       [ 0.132904,  2.295767, -0.213659, -0.220671,  0.252793],\n",
       "       [-0.635167, -0.604269, -0.424825, -0.776152,  1.639352],\n",
       "       [ 1.083637,  0.264546, -0.793833, -2.236928, -0.479406]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2a3db334",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.511263,  0.435096,  0.132904, -0.635167,  1.083637],\n",
       "       [ 1.856601, -1.118819,  2.295767, -0.604269,  0.264546],\n",
       "       [ 1.209131, -0.497072, -0.213659, -0.424825, -0.793833],\n",
       "       [-0.452016, -0.002354, -0.220671, -0.776152, -2.236928],\n",
       "       [ 0.140602, -0.0527  ,  0.252793,  1.639352, -0.479406]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "08c429ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25.340903, -1.057223, 13.648054,  4.284155, 14.851703],\n",
       "       [-1.057223,  0.199332, -0.770614, -0.084179, -0.609982],\n",
       "       [13.648054, -0.770614,  8.082631,  2.056251,  7.999905],\n",
       "       [ 4.284155, -0.084179,  2.056251,  0.9886  ,  2.531245],\n",
       "       [14.851703, -0.609982,  7.999905,  2.531245,  9.040075]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inv(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f69d985",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
